package ntu.metaweb.http;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

import ntu.metaweb.storage.User;
import ntu.metaweb.storage.local.LocalStorageManager;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;

public class RegisterNewUserHandler implements CommandHandler {

	@SuppressWarnings("restriction")
	@Override
	public void handleCommand(HttpExchange exchange,
			HashMap<String, String> params) throws IOException {
		User newUser = 
			new User(params.get("user"), params.get("email"), params.get("password"));
		LocalStorageManager.getInstance().addUserData(newUser);
		
		Headers responseHeaders = exchange.getResponseHeaders();
		responseHeaders.set("Content-Type", "text/xml");

		exchange.sendResponseHeaders(200, 0);

		OutputStream responseBody = exchange.getResponseBody();
		String temp = "<xmlresponse>\n" 
				+ "<returncode>1</returncode>\n"
				+ "</xmlresponse>";
		responseBody.write(temp.getBytes());
		responseBody.close();
		exchange.close();
	}

}
